package com.student.Activitys.utils;

import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.view.ViewCompat;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.student.Activitys.R;
import com.student.Activitys.other.SystemBarTintManager;

import static com.student.Activitys.utils.UiUtil.getStatusBarHeight;


/**
 * 项目名称：
 * 类描述：自定义状态栏 工具类
 * 创建人：张飞祥
 * 创建时间：2017/9/18 下午2:17
 * 修改人：张飞祥
 * 修改时间：2017/9/18 下午2:17
 * 修改备注：
 */
public class StatusBarUtils {

    public static void styleStatusbar(String bar, Activity activity) {
        /**
         *
         * 自定义状态栏UI样式
         *
         * if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
         *      如果机型为6.0  设置状态栏为白色,黑色图标
         * if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
         *      如果机型为6.0 一下 那么设置 为状态栏为透明，底色自定义
         *
         */
        switch (bar){
            //白色
            case Constant.STATUS_WHITE:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    activity.getWindow().getDecorView().setSystemUiVisibility(
                            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                    activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
                }else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    setTranslucentStatus(true,activity);
                    SystemBarTintManager tintManager = new SystemBarTintManager(activity);
                    tintManager.setStatusBarTintEnabled(true);
                    tintManager.setStatusBarTintResource(R.color.colorPrimary);//通知栏所需颜色
                }
                break;
            //默认主题颜色
            case Constant.STATUS_DEFAULT:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    Window window = activity.getWindow();
                    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
                    window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                    window.setStatusBarColor(Color.parseColor("#1565c0"));
                }else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
                    setTranslucentStatus(true,activity);
                    SystemBarTintManager tintManager = new SystemBarTintManager(activity);
                    tintManager.setStatusBarTintEnabled(true);
                    tintManager.setStatusBarTintResource(R.color.colorPrimary);//通知栏所需颜色
                }
                break;
            //黑色
            case Constant.STATUS_BLACK:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    setTranslucentStatus(true,activity);
                    SystemBarTintManager tintManager = new SystemBarTintManager(activity);
                    tintManager.setStatusBarTintEnabled(true);
                    tintManager.setStatusBarTintResource(R.color.windowBackground);//通知栏所需颜色
                }
                break;
        }
    }

    /**
     *
     * 低版本 状态栏 API 19
     *
     */
    @TargetApi(19)
    private static void setTranslucentStatus(boolean on, Activity activity) {
        Window win = activity.getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;

        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    /**
     *
     * 沉溺式状态栏  全屏模式
     *
     */

    public void setAddiction(Activity activity,int color){

        /**
         * 判断当前版本 是否大于或等于 5.0
         */
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
            Window window = activity.getWindow();
            //设置透明状态栏,这样才能让 ContentView 向上
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

            //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            //设置状态栏颜色
            window.setStatusBarColor(color);

            ViewGroup mContentView = (ViewGroup) activity.findViewById(Window.ID_ANDROID_CONTENT);
            View mChildView = mContentView.getChildAt(0);
            if (mChildView != null) {
                //注意不是设置 ContentView 的 FitsSystemWindows, 而是设置 ContentView 的第一个子 View . 使其不为系统 View 预留空间.
                ViewCompat.setFitsSystemWindows(mChildView, false);
            }
        }


        /**
         * 判断当前版本 是否小于或等于 5.0 并且 小于或等于 4.4
         */
        if(Build.VERSION.SDK_INT<Build.VERSION_CODES.LOLLIPOP&&Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT){
            Window window = activity.getWindow();
            ViewGroup mContentView = (ViewGroup) activity.findViewById(Window.ID_ANDROID_CONTENT);

            //首先使 ChildView 不预留空间
            View mChildView = mContentView.getChildAt(0);
            if (mChildView != null) {
                ViewCompat.setFitsSystemWindows(mChildView, false);
            }
            int statusBarHeight = getStatusBarHeight(activity);
            //需要设置这个 flag 才能设置状态栏
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //避免多次调用该方法时,多次移除了 View
            if (mChildView != null && mChildView.getLayoutParams() != null && mChildView.getLayoutParams().height == statusBarHeight) {
                //移除假的 View.
                mContentView.removeView(mChildView);
                mChildView = mContentView.getChildAt(0);
            }
            if (mChildView != null) {
                FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mChildView.getLayoutParams();
                //清除 ChildView 的 marginTop 属性
                if (lp != null && lp.topMargin >= statusBarHeight) {
                    lp.topMargin -= statusBarHeight;
                    mChildView.setLayoutParams(lp);
                }
            }
        }

    }

}
